<?php
// 数据库查询测试脚本
require_once __DIR__ . '/vendor/autoload.php';

// 手动加载Laravel配置
$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

use Illuminate\Support\Facades\DB;
use App\Models\MiniUser;

$token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ0aW1lc3RhbXAiOjE3NTU1MDcwNjZ9.62f419525a010cd2612bb6be8b486ebb08ee2068bc1dbd21585aa4c259a54f7f';
$userId = 1;

echo "数据库查询测试\n";
echo "Token: $token\n";
echo "User ID: $userId\n\n";

try {
    // 1. 查询token记录
    echo "1. 查询mini_tokens表:\n";
    $tokenRecord = DB::table('mini_tokens')
        ->where('token', $token)
        ->first();
    
    if ($tokenRecord) {
        echo "Token记录存在:\n";
        echo "- user_id: {$tokenRecord->user_id}\n";
        echo "- expired_time: {$tokenRecord->expired_time}\n";
        echo "- created_at: {$tokenRecord->created_at}\n";
    } else {
        echo "Token记录不存在!\n";
    }
    
    echo "\n";
    
    // 2. 查询用户记录
    echo "2. 查询mini_users表:\n";
    $user = MiniUser::find($userId);
    
    if ($user) {
        echo "用户记录存在:\n";
        echo "- id: {$user->id}\n";
        echo "- openid: {$user->openid}\n";
        echo "- nickname: {$user->nickname}\n";
        echo "- status: {$user->status}\n";
        echo "- created_at: {$user->created_at}\n";
    } else {
        echo "用户记录不存在!\n";
    }
    
    echo "\n";
    
    // 3. 查询所有mini_users记录
    echo "3. 所有mini_users记录:\n";
    $allUsers = MiniUser::all();
    foreach ($allUsers as $u) {
        echo "- ID: {$u->id}, OpenID: {$u->openid}, Nickname: {$u->nickname}, Status: {$u->status}\n";
    }
    
    echo "\n";
    
    // 4. 查询所有mini_tokens记录
    echo "4. 所有mini_tokens记录:\n";
    $allTokens = DB::table('mini_tokens')->get();
    foreach ($allTokens as $t) {
        echo "- User ID: {$t->user_id}, Expired: {$t->expired_time}, Token: " . substr($t->token, 0, 20) . "...\n";
    }
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}